package com.chonor.mall.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.chonor.mall.dao.PortalProductDao;
import com.chonor.mall.domain.PortalProductDetail;
import com.chonor.mall.domain.ProductCategoryNode;
import com.chonor.mall.mapper.*;
import com.chonor.mall.model.*;
import com.chonor.mall.service.PortalProductService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 前台订单管理Service实现类
 * Created by macro on 2020/4/6.
 */
@Service
public class PortalProductServiceImpl implements PortalProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductCategoryMapper productCategoryMapper;
    @Autowired
    private BrandMapper brandMapper;
    @Autowired
    private ProductAttributeMapper productAttributeMapper;
    @Autowired
    private ProductAttributeValueMapper productAttributeValueMapper;
    @Autowired
    private SkuStockMapper skuStockMapper;
    @Autowired
    private ProductLadderMapper productLadderMapper;
    @Autowired
    private ProductFullReductionMapper productFullReductionMapper;
    @Autowired
    private PortalProductDao portalProductDao;

    @Override
    public List<Product> search(String keyword, Long brandId, Long productCategoryId, Integer pageNum, Integer pageSize, Integer sort) {
        PageHelper.startPage(pageNum, pageSize);
        ProductExample example = new ProductExample();
        ProductExample.Criteria criteria = example.createCriteria();
        criteria.andIsdeleteEqualTo(0);
        if (StrUtil.isNotEmpty(keyword)) {
            criteria.andNameLike("%" + keyword + "%");
        }
        if (brandId != null) {
            criteria.andBrandIdEqualTo(brandId);
        }
        if (productCategoryId != null) {
            criteria.andProductCategoryIdEqualTo(productCategoryId);
        }
        //1->按新品；2->按销量；3->价格从低到高；4->价格从高到低
        if (sort == 1) {
            example.setOrderByClause("id desc");
        } else if (sort == 2) {
            example.setOrderByClause("sale desc");
        } else if (sort == 3) {
            example.setOrderByClause("price asc");
        } else if (sort == 4) {
            example.setOrderByClause("price desc");
        }
        return productMapper.selectByExample(example);
    }

    @Override
    public List<ProductCategoryNode> categoryTreeList() {
        ProductCategoryExample example = new ProductCategoryExample();
        List<ProductCategory> allList = productCategoryMapper.selectByExample(example);
        List<ProductCategoryNode> result = allList.stream()
                .filter(item -> item.getParentId().equals(0L))
                .map(item -> covert(item, allList)).collect(Collectors.toList());
        return result;
    }

    @Override
    public PortalProductDetail detail(Long id) {
        PortalProductDetail result = new PortalProductDetail();
        //获取商品信息
        Product product = productMapper.selectByPrimaryKey(id);
        result.setProduct(product);
        //获取品牌信息
        Brand brand = brandMapper.selectByPrimaryKey(product.getBrandId());
        result.setBrand(brand);
        //TODO 组合属性与属性值，解析JSON格式的sku销售属性
        //获取商品属性信息
//        ProductAttributeExample attributeExample = new ProductAttributeExample();
//        attributeExample.createCriteria().andProductAttributeCategoryIdEqualTo(product.getProductAttributeCategoryId());
//        List<ProductAttribute> productAttributeList = productAttributeMapper.selectByExample(attributeExample);
//        result.setProductAttributeList(productAttributeList);
        //获取商品属性值信息
//        if(CollUtil.isNotEmpty(productAttributeList)){
//            List<Long> attributeIds = productAttributeList.stream().map(ProductAttribute::getId).collect(Collectors.toList());
//            ProductAttributeValueExample attributeValueExample = new ProductAttributeValueExample();
//            attributeValueExample.createCriteria().andProductIdEqualTo(product.getId())
//                    .andProductAttributeIdIn(attributeIds);
//            List<ProductAttributeValue> productAttributeValueList = productAttributeValueMapper.selectByExample(attributeValueExample);
//            result.setProductAttributeValueList(productAttributeValueList);
//        }
        //获取商品SKU库存信息
//        SkuStockExample skuExample = new SkuStockExample();
//        skuExample.createCriteria().andProductIdEqualTo(product.getId());
//        List<SkuStock> skuStockList = skuStockMapper.selectByExample(skuExample);
//        result.setSkuStockList(skuStockList);
        //商品阶梯价格设置
//        if(product.getPromotionType()==3){
//            ProductLadderExample ladderExample = new ProductLadderExample();
//            ladderExample.createCriteria().andProductIdEqualTo(product.getId());
//            List<ProductLadder> productLadderList = productLadderMapper.selectByExample(ladderExample);
//            result.setProductLadderList(productLadderList);
//        }
        //商品满减价格设置
//        if(product.getPromotionType()==4){
//            ProductFullReductionExample fullReductionExample = new ProductFullReductionExample();
//            fullReductionExample.createCriteria().andProductIdEqualTo(product.getId());
//            List<ProductFullReduction> productFullReductionList = productFullReductionMapper.selectByExample(fullReductionExample);
//            result.setProductFullReductionList(productFullReductionList);
//        }
        //商品可用优惠券
        result.setCouponList(portalProductDao.getAvailableCouponList(product.getId(),product.getProductCategoryId()));
        return result;
    }

    @Override
    public List<ProductCategory> category() {
        return productCategoryMapper.selectByExample(new ProductCategoryExample());
    }

    @Override
    public List<Product> categoryProduct(List<Long> ids) {
        ProductExample productExample = new ProductExample();
        if(!Objects.isNull(ids) && ids.size() > 0){
            productExample.createCriteria().andProductCategoryIdIn(ids);
        }
        return productMapper.selectByExample(productExample);
    }


    /**
     * 初始对象转化为节点对象
     * item 顶级分类
     *
     * */
    private ProductCategoryNode covert(ProductCategory item, List<ProductCategory> allList) {
        ProductCategoryNode node = new ProductCategoryNode();
        BeanUtils.copyProperties(item, node);
        List<ProductCategoryNode> children = allList.stream()
                .filter(subItem -> subItem.getParentId().equals(item.getId()))
                .map(subItem -> covert(subItem, allList)).collect(Collectors.toList());
        node.setChildren(children);
        return node;
    }
}
